home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / malloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  8.8 KB  |  339 lines

  1. /*
  2.  * defines for the malloc-debug library
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  *
  24.  * $Id: malloc.h,v 1.21 1993/04/06 04:48:09 gray Exp $
  25.  */
  26.  
  27. #ifndef __MALLOC_H__
  28. #define __MALLOC_H__
  29.  
  30. /*
  31.  * malloc function return codes
  32.  */
  33. #define CALLOC_ERROR        0        /* error from calloc */
  34. #define MALLOC_ERROR        0        /* error from malloc */
  35. #define REALLOC_ERROR        0        /* error from realloc */
  36.  
  37. #define FREE_ERROR        0        /* error from free */
  38. #define FREE_NOERROR        1        /* no error from free */
  39.  
  40. #define MALLOC_VERIFY_ERROR    0        /* checks failed, error */
  41. #define MALLOC_VERIFY_NOERROR    1        /* checks passed, no error */
  42.  
  43. /*
  44.  * default values if _malloc_file and _malloc_line are not set
  45.  */
  46. #define MALLOC_DEFAULT_FILE    "unknown"
  47. #define MALLOC_DEFAULT_LINE    0
  48.  
  49. /*
  50.  * global variable and procedure scoping for code readability
  51.  */
  52. #undef    EXPORT
  53. #define    EXPORT
  54.  
  55. #undef    IMPORT
  56. #define    IMPORT        extern
  57.  
  58. #undef    LOCAL
  59. #define    LOCAL        static
  60.  
  61. #if __GNUC__ < 2
  62. /*
  63.  * prototype for memory copy.  needed for below macros.
  64.  */
  65. IMPORT    char    *memcpy(char * to, char * from, int length);
  66. #endif
  67.  
  68. /*
  69.  * memory copy: copy SIZE bytes from pointer FROM to pointer TO
  70.  */
  71. #define MEMORY_COPY(from, to, size)    (void)memcpy((char *)to, \
  72.                              (char *)from, size)
  73.  
  74. /*
  75.  * alloc macros to improve memory usage readibility...
  76.  */
  77. #undef ALLOC
  78. #undef CALLOC
  79. #undef MALLOC
  80. #undef REALLOC
  81. #undef REMALLOC
  82. #undef FREE
  83.  
  84. #define ALLOC(type, count) \
  85.   (type *)malloc((unsigned int)(sizeof(type) * (count)))
  86.  
  87. #define MALLOC(size) \
  88.   (char *)malloc((unsigned int)(size))
  89.  
  90. /* WARNING: notice that the arguments are REVERSED from normal calloc() */
  91. #define CALLOC(type, count) \
  92.   (type *)calloc((unsigned int)(count), (unsigned int)sizeof(type))
  93.  
  94. #define REALLOC(ptr, type, count) \
  95.   (type *)realloc((char *)(ptr), (unsigned int)(sizeof(type) * (count)))
  96.  
  97. #define REMALLOC(ptr, size) \
  98.   (char *)realloc((char *)(ptr), (unsigned int)(size))
  99.  
  100. #define FREE(ptr) \
  101.   free((char *)(ptr))
  102.  
  103. /*
  104.  * some small allocation macros
  105.  */
  106.  
  107. #ifdef __GNUC__
  108.  
  109. /*
  110.  * duplicate BUF of SIZE bytes
  111.  */
  112. #define BDUP(buf, size)    ({ \
  113.               char    *_ret; \
  114.               int    _size = (size); \
  115.                \
  116.               _ret = MALLOC(_size); \
  117.               if (_ret != NULL) \
  118.                 MEMORY_COPY((buf), _ret, _size); \
  119.                \
  120.               _ret; \
  121.             })
  122.  
  123. /*
  124.  * the strdup() function in macro form.  duplicate string STR
  125.  */
  126. #define STRDUP(str)    ({ \
  127.               const char *_strp = (str); \
  128.               char    *_ret; \
  129.               int    _len; \
  130.                \
  131.               _len = strlen(_strp); \
  132.               _ret = MALLOC(_len + 1); \
  133.               if (_ret != NULL) \
  134.                 MEMORY_COPY(_strp, _ret, _len + 1); \
  135.                \
  136.               _ret; \
  137.             })
  138.  
  139. #else /* ! __GNUC__ */
  140.  
  141. /*
  142.  * duplicate BUF of SIZE and return the new address in OUT
  143.  */
  144. #define BDUP(buf, size, out)    do { \
  145.                   char    *_ret; \
  146.                   int    _size = (size); \
  147.                    \
  148.                   _ret = MALLOC(_size); \
  149.                   if (_ret != NULL) \
  150.                     MEMORY_COPY((buf), _ret, _size); \
  151.                    \
  152.                   (out) = _ret; \
  153.                 } while(0)
  154.  
  155. /*
  156.  * strdup() in macro form.  duplicate string STR and return a copy in OUT
  157.  */
  158. #define STRDUP(str, out)    do { \
  159.                   const char *_strp = (str); \
  160.                   char    *_ret; \
  161.                   int    _len; \
  162.                    \
  163.                   _len = strlen(_strp); \
  164.                   _ret = MALLOC(_len + 1); \
  165.                   if (_ret != NULL) \
  166.                     MEMORY_COPY(_strp, _ret, _len + 1); \
  167.                    \
  168.                   (out) = _ret; \
  169.                 } while(0)
  170.  
  171. #endif /* ! __GNUC__ */
  172.  
  173. /*<<<<<<<<<<  The below prototypes are auto-generated by fillproto */
  174.  
  175. /* logfile for dumping malloc info, MALLOC_LOGFILE env. var overrides this */
  176. IMPORT    char        *malloc_logpath;
  177.  
  178. /* internal malloc error number for reference purposes only */
  179. IMPORT    int        malloc_errno;
  180.  
  181. /*
  182.  * shutdown memory-allocation module, provide statistics if necessary
  183.  */
  184. IMPORT    void    malloc_shutdown(void);
  185.  
  186. /*
  187.  * allocate and return a SIZE block of bytes
  188.  */
  189. IMPORT    char    *malloc(unsigned int size);
  190.  
  191. /*
  192.  * allocate and return a block of bytes able to hold NUM_ELEMENTS of elements
  193.  * of SIZE bytes and zero the block
  194.  */
  195. IMPORT    char    *calloc(unsigned int num_elements, unsigned int size);
  196.  
  197. /*
  198.  * resizes OLD_PNT to SIZE bytes and return the new space after either copying
  199.  * all of OLD_PNT to the new area or truncating
  200.  */
  201. IMPORT    char    *realloc(char * old_pnt, unsigned int new_size);
  202.  
  203. /*
  204.  * release PNT in the heap, returning FREE_[NO]ERROR
  205.  */
  206. IMPORT    int    free(char * pnt);
  207.  
  208. /*
  209.  * call through to _heap_map function, returns [NO]ERROR
  210.  */
  211. IMPORT    int    malloc_heap_map(void);
  212.  
  213. /*
  214.  * verify pointer PNT, if PNT is 0 then check the entire heap.
  215.  * returns MALLOC_VERIFY_[NO]ERROR
  216.  */
  217. IMPORT    int    malloc_verify(char * pnt);
  218.  
  219. /*
  220.  * set the global debug functionality flags to DEBUG (0 to disable).
  221.  * returns [NO]ERROR
  222.  */
  223. IMPORT    int    malloc_debug(int debug);
  224.  
  225. /*
  226.  * examine pointer PNT and returns SIZE, and FILE / LINE info on it
  227.  * if any of the pointers are not NULL.
  228.  * returns NOERROR or ERROR depending on whether PNT is good or not
  229.  */
  230. IMPORT    int    malloc_examine(char * pnt, unsigned int * size,
  231.                    char ** file, unsigned int * line);
  232.  
  233. /*
  234.  * malloc version of strerror to return the string version of ERRNUM
  235.  * returns the string for MALLOC_BAD_ERRNO if ERRNUM is out-of-range.
  236.  */
  237. IMPORT    char    *malloc_strerror(int errnum);
  238.  
  239. /*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */
  240.  
  241. /*
  242.  * alloc macros to provide for memory FILE/LINE debugging information.
  243.  */
  244.  
  245. #ifndef MALLOC_DEBUG_DISABLE
  246.  
  247. #define malloc(size) \
  248.   _malloc_leap(__FILE__, __LINE__, size)
  249. #define calloc(count, size) \
  250.   _calloc_leap(__FILE__, __LINE__, count, size)
  251. #define realloc(ptr, size) \
  252.   _realloc_leap(__FILE__, __LINE__, ptr, size)
  253. #define free(ptr) \
  254.   _free_leap(__FILE__, __LINE__, ptr)
  255.  
  256. #ifdef MALLOC_FUNC_CHECK
  257.  
  258. /*
  259.  * do debugging on the following functions.  this may cause compilation or
  260.  * other problems depending on your architecture, hence the need for the
  261.  * MALLOC_STRING_DISABLE define.
  262.  */
  263. #define bcmp(b1, b2, len)        _malloc_bcmp(b1, b2, len)
  264. #define bcopy(from, to, len)        _malloc_bcopy(from, to, len)
  265.  
  266. #define memcmp(b1, b2, len)        _malloc_memcmp(b1, b2, len)
  267. #define memcpy(to, from, len)        _malloc_memcpy(to, from, len)
  268. #define memset(buf, ch, len)        _malloc_memset(buf, ch, len)
  269.  
  270. #define index(str, ch)            _malloc_index(str, ch)
  271. #define rindex(str, ch)            _malloc_rindex(str, ch)
  272.  
  273. #define strcat(to, from)        _malloc_strcat(to, from)
  274. #define strcmp(s1, s2)            _malloc_strcmp(s1, s2)
  275. #define strlen(str)            _malloc_strlen(str)
  276. #define strtok(str, sep)        _malloc_strtok(str, sep)
  277.  
  278. #define bzero(buf, len)            _malloc_bzero(buf, len)
  279.  
  280. #define memccpy(s1, s2, ch, len)    _malloc_memccpy(s1, s2, ch, len)
  281. #define memchr(s1, ch, len)        _malloc_memchr(s1, ch, len)
  282.  
  283. #define strchr(str, ch)            _malloc_strchr(str, ch)
  284. #define strrchr(str, ch)        _malloc_strrchr(str, ch)
  285.  
  286. #define strcpy(to, from)        _malloc_strcpy(to, from)
  287. #define strncpy(to, from, len)        _malloc_strncpy(to, from, len)
  288. #define strcasecmp(s1, s2)        _malloc_strcasecmp(s1, s2)
  289. #define strncasecmp(s1, s2, len)    _malloc_strncasecmp(s1, s2, len)
  290. #define strspn(str, list)        _malloc_strspn(str, list)
  291. #define strcspn(str, list)        _malloc_strcspn(str, list)
  292. #define strncat(to, from, len)        _malloc_strncat(to, from, len)
  293. #define strncmp(s1, s2, len)        _malloc_strncmp(s1, s2, len)
  294. #define strpbrk(str, list)        _malloc_strpbrk(str, list)
  295. #define strstr(str, pat)        _malloc_strstr(str, pat)
  296.  
  297. /*
  298.  * feel free to add your favorite functions here and to malloc_str.[ch]
  299.  */
  300.  
  301. #endif /* MALLOC_FUNC_CHECK */
  302.  
  303. /*
  304.  * copied directly from malloc_lp.h
  305.  */
  306.  
  307. /* to inform the malloc-debug library from which file the call comes from */
  308. IMPORT    char        *_malloc_file;
  309.  
  310. /* to inform the library from which line-number the call comes from */
  311. IMPORT    unsigned int    _malloc_line;
  312.  
  313. /*
  314.  * leap routine to calloc
  315.  */
  316. IMPORT    char    *_calloc_leap(const char * file, const int line,
  317.                   unsigned int elen, unsigned int size);
  318.  
  319. /*
  320.  * leap routine to free
  321.  */
  322. IMPORT    int    _free_leap(const char * file, const int line, char * pnt);
  323.  
  324. /*
  325.  * leap routine to malloc
  326.  */
  327. IMPORT    char    *_malloc_leap(const char * file, const int line,
  328.                   unsigned int size);
  329.  
  330. /*
  331.  * leap routine to realloc
  332.  */
  333. IMPORT    char    *_realloc_leap(const char * file, const int line, char * oldp,
  334.                    unsigned int new_size);
  335.  
  336. #endif /* MALLOC_DEBUG_DISABLE */
  337.  
  338. #endif /* ! __MALLOC_H__ */
  339.